#include "Prims.h"

using namespace slak::mesh;

Patch * Prims::makeBoxPatch(float cx,
			    float cy,
			    float cz,
			    float width,
			    float height,
			    float depth)
{
	Patch * box = new Patch();

	box->draw_mode = GL_QUADS;
	box->vertex_count = 24;
	box->vertices = new float[24*3];
	int box_vindices[] = {
		0, 2, 6, 4,
		5, 7, 3, 1,
		4, 6, 7, 5,
		1, 3, 2, 0,
		2, 3, 7, 6,
		4, 5, 1, 0,
	};
	float box_vertices[8][3] = {
		{cx - width/2, cy - height/2, cz - depth/2},
		{cx - width/2, cy - height/2, cz + depth/2},
		{cx - width/2, cy + height/2, cz - depth/2},
		{cx - width/2, cy + height/2, cz + depth/2},
		{cx + width/2, cy - height/2, cz - depth/2},
		{cx + width/2, cy - height/2, cz + depth/2},
		{cx + width/2, cy + height/2, cz - depth/2},
		{cx + width/2, cy + height/2, cz + depth/2},
	};
	for(int v=0; v<24; v++) 
		for(int i=0; i<3; i++)
			box->vertices[v*3 + i] = 
				box_vertices[box_vindices[v]][i];

	box->normals = new float[24*3];
	float face_normals[][3] = {
		{ 0, 0,-1},
		{ 0, 0, 1},
		{ 1, 0, 0},
		{-1, 0, 0},
		{ 0, 1, 0},
		{ 0,-1, 0}
	};
	for(int face=0; face<6; face++)
		for(int v=0; v<4; v++)
			for(int i=0; i<3; i++)
				box->normals[face*4*3 + v*3 + i] = 
					face_normals[face][i];

	return box;	
}
